home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15149 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  51 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!smryan
  3. From: smryan@netcom.com (@#$%!?!)
  4. Subject: Re: Jobs running under Unix.
  5. Message-ID: <smryanDpzq59.75q@netcom.com>
  6. Organization: The Programmer formerly known as S M Ryan
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <829283553snz@tarik.demon.co.uk>
  9. Date: Wed, 17 Apr 1996 05:05:33 GMT
  10. Sender: smryan@netcom8.netcom.com
  11.  
  12. : Is there anyone who knows how to tell whether a job is running under Unix
  13. : from within a C program.
  14.  
  15. : e.g. how do I stop a program from running more than once ?
  16.  
  17. Rather than directly interrogating running process, try an indirect, and
  18. time honorred technique: lock files.
  19.  
  20. Have the program in its initialisation try to lock a file. If it fails, it
  21. stops immediately. If it does lock the file, let it continue. When it
  22. completes, remove the lock if you want to permit the program to run again,
  23. or leave the lock to prevent reruns until the lock is explicitly cleared.
  24.  
  25. On Unix you can lock with either exclusive create or locking file contents.
  26. These, or variants of them, can also be used on other systems.
  27.  
  28. When you create a file, you can mark it as an exclusive create. If the file
  29. already exists, the create fails. If the file does not exist, the create
  30. succeeds. Subsequent exclusive creates fail until the file is unlinked. This
  31. lock is not associated with a specific process and can be useful when
  32. locking shell scripts or complex programs. The operating systems serialises
  33. create so simultaneous creates will only be satisfied by one caller.
  34.  
  35. You can also lock a file contents using fcntl(2). Write lock the entire file
  36. content. (You're going to have study the man pages.) This lock is process
  37. based so that if your program ends or aborts without clearing the lock, the
  38. operating system clears it anyway. Also you can have your program suspended
  39. until the process holding the lock clears it.
  40.  
  41. Unix provides other techniques which are not always found on other kinds of
  42. operating systems, nor always compatiable from one Unix box to another.
  43. These include shared memory semaphores, message queues, and busy locks or
  44. existence checks of shared segments. There are also various manners of
  45. process locks.
  46. -- 
  47. The Queen who loves, the Queen of life,    | smryan@netcom.com  PO Box 1563
  48. the Queen who straits, the Queen of strife;|          Cupertino, California
  49. with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
  50. she brings the choice of birth or knife.   |         I don't use no smileys
  51.